﻿using System;
using System.Collections.Generic;
using UniRx.InternalUtil;

namespace UniRx
{
	// Token: 0x0200022B RID: 555
	public class AsyncMessageBroker : IAsyncMessageBroker, IDisposable, IAsyncMessagePublisher, IAsyncMessageReceiver
	{
		// Token: 0x06000D06 RID: 3334 RVA: 0x0003ED84 File Offset: 0x0003D184
		public IObservable<Unit> PublishAsync<T>(T message)
		{
			object obj = this.notifiers;
			ImmutableList<Func<T, IObservable<Unit>>> immutableList;
			lock (obj)
			{
				if (this.isDisposed)
				{
					throw new ObjectDisposedException("AsyncMessageBroker");
				}
				object obj2;
				if (!this.notifiers.TryGetValue(typeof(T), out obj2))
				{
					return Observable.ReturnUnit();
				}
				immutableList = (ImmutableList<Func<T, IObservable<Unit>>>)obj2;
			}
			Func<T, IObservable<Unit>>[] data = immutableList.Data;
			IObservable<Unit>[] array = new IObservable<Unit>[data.Length];
			for (int i = 0; i < data.Length; i++)
			{
				array[i] = data[i](message);
			}
			return Observable.WhenAll(array);
		}

		// Token: 0x06000D07 RID: 3335 RVA: 0x0003EE48 File Offset: 0x0003D248
		public IDisposable Subscribe<T>(Func<T, IObservable<Unit>> asyncMessageReceiver)
		{
			object obj = this.notifiers;
			lock (obj)
			{
				if (this.isDisposed)
				{
					throw new ObjectDisposedException("AsyncMessageBroker");
				}
				object obj2;
				if (!this.notifiers.TryGetValue(typeof(T), out obj2))
				{
					ImmutableList<Func<T, IObservable<Unit>>> immutableList = ImmutableList<Func<T, IObservable<Unit>>>.Empty;
					immutableList = immutableList.Add(asyncMessageReceiver);
					this.notifiers.Add(typeof(T), immutableList);
				}
				else
				{
					ImmutableList<Func<T, IObservable<Unit>>> immutableList2 = (ImmutableList<Func<T, IObservable<Unit>>>)obj2;
					immutableList2 = immutableList2.Add(asyncMessageReceiver);
					this.notifiers[typeof(T)] = immutableList2;
				}
			}
			return new AsyncMessageBroker.Subscription<T>(this, asyncMessageReceiver);
		}

		// Token: 0x06000D08 RID: 3336 RVA: 0x0003EF08 File Offset: 0x0003D308
		public void Dispose()
		{
			object obj = this.notifiers;
			lock (obj)
			{
				if (!this.isDisposed)
				{
					this.isDisposed = true;
					this.notifiers.Clear();
				}
			}
		}

		// Token: 0x04000756 RID: 1878
		public static readonly IAsyncMessageBroker Default = new AsyncMessageBroker();

		// Token: 0x04000757 RID: 1879
		private bool isDisposed;

		// Token: 0x04000758 RID: 1880
		private readonly Dictionary<Type, object> notifiers = new Dictionary<Type, object>();

		// Token: 0x0200022C RID: 556
		private class Subscription<T> : IDisposable
		{
			// Token: 0x06000D0A RID: 3338 RVA: 0x0003EF68 File Offset: 0x0003D368
			public Subscription(AsyncMessageBroker parent, Func<T, IObservable<Unit>> asyncMessageReceiver)
			{
				this.parent = parent;
				this.asyncMessageReceiver = asyncMessageReceiver;
			}

			// Token: 0x06000D0B RID: 3339 RVA: 0x0003EF80 File Offset: 0x0003D380
			public void Dispose()
			{
				object notifiers = this.parent.notifiers;
				lock (notifiers)
				{
					object obj;
					if (this.parent.notifiers.TryGetValue(typeof(T), out obj))
					{
						ImmutableList<Func<T, IObservable<Unit>>> immutableList = (ImmutableList<Func<T, IObservable<Unit>>>)obj;
						immutableList = immutableList.Remove(this.asyncMessageReceiver);
						this.parent.notifiers[typeof(T)] = immutableList;
					}
				}
			}

			// Token: 0x04000759 RID: 1881
			private readonly AsyncMessageBroker parent;

			// Token: 0x0400075A RID: 1882
			private readonly Func<T, IObservable<Unit>> asyncMessageReceiver;
		}
	}
}
